package com.ljh.account.mapper;

import com.ljh.account.dao.BankAccountBinding;
import com.ljh.account.dao.BankPartner;
import com.ljh.account.dao.Resp.BankAccountBindingResp;
import org.apache.ibatis.annotations.*;

import java.util.List;

import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface BankAccountBindingMapper {

    // 插入
    @Insert("INSERT INTO bank_account_bindings (partner_id, account_holder_name, id_card_number, bank_account_number, account_usage, user_id) " +
            "VALUES (#{partnerId}, #{accountHolderName}, #{idCardNumber}, #{bankAccountNumber}, #{accountUsage}, #{userId})")
    void insert(BankAccountBinding binding);

    // 删除
    @Delete("DELETE FROM bank_account_bindings WHERE id = #{id}")
    void deleteById(@Param("id") Integer id);

    // 更新
    @Update("UPDATE bank_account_bindings SET partner_id = #{partnerId}, account_holder_name = #{accountHolderName}, " +
            "id_card_number = #{idCardNumber}, bank_account_number = #{bankAccountNumber}, account_usage = #{accountUsage}, user_id = #{userId} " +
            "WHERE id = #{id}")
    void update(BankAccountBinding binding);


    // 根据ID查询
    @Select("SELECT * FROM bank_account_bindings WHERE id = #{id}")
    BankAccountBinding findById(@Param("id") Integer id);

    // 筛选功能
    List<BankAccountBinding> filter(@Param("partnerId") Integer partnerId,
                                    @Param("userId") Integer userId,
                                    @Param("accountHolderName") String accountHolderName);
    // 分页查询
//    @Select("SELECT * FROM bank_account_bindings WHERE user_id = #{userId} LIMIT #{limit} OFFSET #{offset} ")
//    List<BankAccountBinding> findAll(@Param("userId") Long userId,@Param("offset") int offset, @Param("limit") int limit);

    // 多表联查，并将数据写入实体类
    @Select("SELECT *" +
            "FROM bank_account_bindings b " +
            "JOIN bank_partners a ON b.partner_id = a.id " +
            "WHERE b.user_id = #{userId} ")
    @Results({
            @Result(property = "bankName", column = "bank_name"),
            @Result(property = "coverImage", column = "cover_image"),
    })
    List<BankAccountBindingResp> findAll(@Param("userId") Long userId);

}